package com.example.androidproject.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

import com.example.androidproject.app.MainActivity;
import com.example.androidproject.utils.GlobalVariable;

import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.security.KeyStore;
import java.util.Calendar;

public abstract class ConnectionChangeReceiver extends BroadcastReceiver {
    private static final String TAG = "GGSTUDY";
    private boolean allContent = false;

    public ConnectionChangeReceiver() {
    }

    public static boolean isWiFiEnable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return !((info == null) || (!info.isAvailable())) && info.getType() == ConnectivityManager.TYPE_WIFI;
    }

    public static boolean isMobileEnable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return !((info == null) || (!info.isAvailable())) && info.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    public static boolean isMobileEnableReflex(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Method getMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
            getMobileDataEnabledMethod.setAccessible(true);
            return (Boolean) getMobileDataEnabledMethod.invoke(connectivityManager);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isAllEnable(Context context) {
        return isWiFiEnable(context) && isMobileEnableReflex(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//        if (networkInfo != null && networkInfo.isAvailable()) {
//            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//                Log.d(TAG, "onReceive: wifi");
//            }
//            if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
//                Log.d(TAG, "onReceive: 数据网络");
//                Toast.makeText(context, "当前正在使用数据", Toast.LENGTH_SHORT).show();
//            }
//        } else {
//            Log.d(TAG, "onReceive: 当前网络不可用");
//        }
        try {
            if (ConnectionChangeReceiver.isAllEnable(context)) {
                allContent = true;
            }
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            // 判断是否为连接状态
            boolean wifiConnected = false;
            boolean mobConnected = false;
            if (wifiNetInfo != null && wifiNetInfo.isConnected() && wifiNetInfo.isAvailable()) {
                wifiConnected = true;
            }
            if (mobNetInfo != null && mobNetInfo.isConnected() && mobNetInfo.isAvailable()) {
                Toast.makeText(context, "正在使用数据", Toast.LENGTH_SHORT).show();
                mobConnected = true;
            }
            if (allContent) {
                if (wifiConnected || mobConnected) {
                    if (mobConnected) {
                        allContent = false;
                        return;
                    }
                    onConnected();
                    return;
                }
            } else {
                if (wifiConnected || mobConnected) {
                    onConnected();
                    return;
                }
            }
            // 判断是否是Disconnected事件，注意：处于中间状态的事件不上报给应用！上报会影响体验
            boolean wifiDisconnected = false;
            boolean mobDisconnected = false;
            if (wifiNetInfo == null || wifiNetInfo != null && wifiNetInfo.getState() == NetworkInfo.State.DISCONNECTED) {
                wifiDisconnected = true;
            }
            if (mobNetInfo == null || mobNetInfo != null && mobNetInfo.getState() == NetworkInfo.State.DISCONNECTED) {
                mobDisconnected = true;
            }
            if (wifiDisconnected && mobDisconnected) {
                onDisconnected();
                return;
            }
        } catch (Exception e) {
            Log.d(TAG, "onReceive: 错误" + e.getMessage());
        }
    }

    protected abstract void onDisconnected();

    protected abstract void onConnected();
}